import { ShopeeForderModel, ShopeeOrderModel } from '../models/shopee.models'
import CommonHelper from '../utils/common.helper'
import { PlatformBaseManager } from './platform-base'

export class ShopeeOrderManager extends PlatformBaseManager {
  init() {
    let exportContainer = $(`
    <div class="export-container">
        <input type="number" value='3'  />
    </div>`)
    exportContainer.appendTo(this.viewContainer)
    jQuery('<button class="btn btn-primary">导出订单</button>')
      .on('click', () => {
        let pageSize = exportContainer.find('input').prop('value')
        this.getOrderList(pageSize).then((orderList) => {
          if (orderList.length) {
            this.saveOrderList(orderList)
          }
        })
      })
      .appendTo(exportContainer)
  }

  getOrderList(pageSize = 40, pageNumber = 1): Promise<OrderInfo[]> {
    let queryString = CommonHelper.toQueryString({
      SPC_CDS: '8655ecc6-a001-4356-bab2-f518bc4fd592',
      SPC_CDS_VER: 2,
      page_size: pageSize,
      page_number: pageNumber,
      total: 0,
      sort_by: 'confirmed_date_desc',
    })
    return fetch(
      'https://seller.my.shopee.cn/api/v3/order/get_forder_list' + queryString
    )
      .then((resp) => resp.json())
      .then((resp) => resp.data.forders)
      .then((forderList: ShopeeForderModel[]) => {
        return Promise.all(
          forderList.map((forder) => {
            let orderQueryString = CommonHelper.toQueryString({
              SPC_CDS: '8655ecc6-a001-4356-bab2-f518bc4fd592',
              SPC_CDS_VER: 2,
              order_id: forder.order_id,
            })
            return fetch(
              'https://seller.my.shopee.cn/api/v3/order/get_one_order' +
                orderQueryString
            )
              .then((resp) => resp.json())
              .then((resp) => {
                let order: ShopeeOrderModel = resp.data
                let [
                  addressState,
                  addressZipcode,
                  addressCity,
                  //   addressDistrice,
                ] = order.shipping_address.split(', ').reverse()
                // if (rest.length === 0) {
                //   addressDistrice = ''
                // }
                return {
                  orderSn: forder.order_sn,
                  trackingNo: forder.third_party_tn,
                  buyerName: order.buyer_address_name,
                  buyerAddress: order.shipping_address,
                  buyerPhone: order.buyer_address_phone,
                  addressState,
                  addressCity,
                  addressDistrice: '',
                  addressZipcode,
                  amount: order.order_items.reduce(
                    (result, item) => result + item.amount,
                    0
                  ),
                  totalPrice: order.total_price,
                  currency: order.currency === 'MYR' ? 'RM' : order.currency,
                  productUrl: `https://shopee.com.my/product/${order.shop_id}/${order.order_items[0].item_id}`,
                }
              })
          })
        )
      })
  }
}
